package test.socket;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.junit.Test;

import hyl.base.net.iot.ABsrv;
import hyl.base.net.iot.Conf;
import hyl.base.net.iot.Csrv;

import hyl.base.net.iot.Netpg3;
import hyl.core.MyCharset;
import hyl.core.MyDate;
import hyl.core.MyFun;
import hyl.core.net.NioClient;

public class testiotcli {
	InetSocketAddress addrc=new InetSocketAddress("127.0.0.1",9090);
	InetSocketAddress addrb=new InetSocketAddress("127.0.0.1",9080);
	InetSocketAddress addra=new InetSocketAddress("127.0.0.1",9070);


	
	NioClient cliD ;
	public void startD() {
		cliD = new NioClient(addrc);		
		cliD.setHandler((read, data) -> {			
			String cmd=new String(data,Conf.D编码);
			System.out.println("测试D:收到C端发送的信息: "+cmd);
			if (cmd.startsWith("did")) {
				return false;
			}
			d的应答(cmd.replace(';', '?')+"ok;");
			return false;
		});
		cliD.start();
		byte[] dname = MyFun.str2Bytes("4001", Conf.D编码);
		try {
			cliD.send(dname);
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}

	public void d的应答(String cmd) {	
		byte[] fk= MyFun.str2Bytes(cmd, Conf.D编码);
		try {
			cliD.send(fk);
			System.out.println("应答:"+cmd);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		testiotcli p3 =new testiotcli();
		p3.startD() ;
	}
	
	
}
